import { defineStore } from 'pinia'
import { RouteRecordRaw } from 'vue-router'
import { getMenuListApi } from '@/api/menu/menu'
import Layout from '@/layout/index.vue'
import Center from '@/layout/center/center.vue'
const modules = import.meta.glob('../views/**/*.vue')
//定义state类型
export type MenuState = {
  //菜单
  menuList: any
}
export const menuStore = defineStore('menuStore', {
  state: (): MenuState => {
    return {
      menuList: [
        //初始化一个首页
        {
          path: '/dashboard',
          component: 'Layout',
          meta: {
            title: '首页',
            icon: 'HomeFilled',
            roles: ['sys:manage']
          },
          children: []
        }
      ]
    }
  },
  actions: {
    // 获取菜单
    getMenuList(router: any) {
      return new Promise((resolve, reject) => {
        getMenuListApi()
          .then((res) => {
            let accessedRoutes
            if (res.code == 200) {
              //动态生成路由
              accessedRoutes = filterAsyncRoutes(res.data, router)
              //设置到menuList
              this.menuList = this.menuList.concat(accessedRoutes)
            }
            //返回
            resolve(accessedRoutes)
          })
          .catch((error) => {
            reject(error)
          })
      })
    }
  },
  getters: {},
  persist: {
    enabled: true //开启持久化
  }
})
export function filterAsyncRoutes(routes: RouteRecordRaw[], router: any) {
  const res: Array<RouteRecordRaw> = []
  routes.forEach((route: any) => {
    const tmp = { ...route }
    const component = tmp.component
    if (route.component) {
      if (component == 'Layout') {
        tmp.component = Layout
      } else {
        tmp.component = modules[`../views${component}.vue`]
      }
    }
    if (tmp.children && tmp.children.length > 0) {
      //如果有下级，并且component != 'Layout'
      if (route.component != 'Layout') {
        tmp.component = Center
      }
      //递归
      tmp.children = filterAsyncRoutes(tmp.children, router)
    }
    router.addRoute(tmp)
    res.push(tmp)
  })
  return res
}
